package com.rui.study.demo.httpclient;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @program: study_20180903
 * @description:
 * @author: Yaowr
 * @create: 2018-10-30 16:00
 **/
@RestController
public class ElasticController {

    @Autowired
    private CoordinatorRegistryCenter registryCenter;

    @Autowired
    private MyElasticJob myElasticJob;


    @GetMapping("/children")
    public Object hello() {
        return registryCenter.getChildrenKeys("/");
    }

    @GetMapping("/add")
    public Object add(String name, @RequestParam(required = false) String cron) {

        if (cron == null || cron.length() <= 0) {
            cron = CronUtil.getCron(new Date(System.currentTimeMillis() + 30000));
        }
        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(name, cron, 1).build();
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, StaticjobDemo.class.getCanonicalName());
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();

        SpringJobScheduler scheduler = new SpringJobScheduler(myElasticJob, registryCenter, simpleJobRootConfig, myElasticJob);
        scheduler.init();

        return "jobname: " + name + ", cron: " + cron;
    }
}
